package com.ryan.banner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.banner.entity.Banner;
import com.ryan.banner.mapper.BannerMapper;
import com.ryan.banner.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author ryan
 * @since 2020-07-28
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    /**
     * 分页+条件查询所有banner
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Banner> queryPage(long pageIndex, long pageSize, String name) {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("gmt_create");

        Page<Banner> bannerPage = new Page<>(pageIndex, pageSize);
        baseMapper.selectPage(bannerPage, wrapper);
        return bannerPage;
    }

    /**
     * 前台页面获取已选中的轮播图，并存放到redis中
     * @return
     */
    @Override
    @Cacheable(value = "bannerList", key = "'getIndexBanner'")
    public List<Banner> getIndexBanner() {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("sort");
        wrapper.last("limit 3");
        List<Banner> bannerList = baseMapper.selectList(wrapper);
        return bannerList;
    }
}
